package com.hsj.reader.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hsj.reader.entity.*;
import com.hsj.reader.service.BookService;
import com.hsj.reader.service.CategoryService;
import com.hsj.reader.service.EvaluationService;

import com.hsj.reader.service.MemberService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

//  controller 层   承上启下

// CategoryService-----接口名
// categoryService-----CategoryServiceImpl 里的  @Service("categoryService") //bean 这里与接口对照

@Controller
public class BookController {
    @Resource
    private CategoryService categoryService;
    @Resource
    private BookService bookService;
    @Resource
    private EvaluationService evaluationService;


    @Resource
    private MemberService memberService;
//查询所有 category
    @GetMapping ("/")
    //    / 代表根路径
    public ModelAndView showIndex(){
        ModelAndView mav = new ModelAndView("/index");
        List<Category> categoryList = categoryService.selectAll();
        mav.addObject("categoryList",categoryList);
        return mav;
    }

    @GetMapping("/books")
    @ResponseBody
    public IPage<Book> selectBook(Long categoryId, String order,Integer p){
        if(p==null){
            p=1;  //如果没有传来数据 默认访问第一页
        }
        IPage<Book> pageObject = bookService.paging(categoryId,order,p,10);
        return pageObject;
    }

    @GetMapping("/book/{id}")
    public ModelAndView showDetail(@PathVariable("id") Long id, HttpSession session){
            Book book =  bookService.selectById(id);
            List<Evaluation> evaluationList = evaluationService.selectByBookId(id);
            Member member =(Member)session.getAttribute("loginMember");
             ModelAndView mav = new ModelAndView("/detail");
            if(member != null){
               MemberReadState memberReadState  = memberService.selectMemberReadState(member.getMemberId(),id);
                    mav.addObject(
                            "memberReadState",memberReadState);
            }



           mav.addObject("book",book);
           mav.addObject("evaluationList",evaluationList);
           return mav;
    }



}
